package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.WechatMchntInfoMapper;
import cc.rengu.igas.bomp.common.entity.WechatMchntInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * Created by 王成 on 2020/3/2
 */
public class WechatMchntInfoMapperImpl implements WechatMchntInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertWechatMchntInfo(WechatMchntInfo wechatMchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertBompWechatMchntInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, wechatMchntInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记微信商户信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateWechatMchntInfoById(WechatMchntInfo wechatMchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompWechatMchntInfoById");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, wechatMchntInfo, wechatMchntInfo);
        if (0 != iReturnCode) {
            rglog.error("更新微信商户信息失败,instId:<{}>,mchntNo:<{}>,id:<{}>,iReturnCode:<{}>", wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), wechatMchntInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateWechatMchntInfoNoTransactionById(WechatMchntInfo wechatMchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompWechatMchntInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, wechatMchntInfo, wechatMchntInfo);
        if (0 != iReturnCode) {
            rglog.error("更新微信商户信息失败,instId:<{}>,mchntNo:<{}>,id:<{}>,iReturnCode:<{}>", wechatMchntInfo.getInstId(), wechatMchntInfo.getMchntNo(), wechatMchntInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public WechatMchntInfo selectWechatMchntInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompWechatMchntInfoById");
        WechatMchntInfo wechatMchntInfo = new WechatMchntInfo();
        wechatMchntInfo.setInstId(instId);
        wechatMchntInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, wechatMchntInfo, wechatMchntInfo);
        if (0 != iReturnCode) {
            rglog.error("获取微信商户基本信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return wechatMchntInfo;
    }

    @Override
    public WechatMchntInfo selectWechatMchntInfoByPrimaryKey(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompWechatMchntInfoByPrimaryKey");
        WechatMchntInfo wechatMchntInfo = new WechatMchntInfo();
        wechatMchntInfo.setInstId(instId);
        wechatMchntInfo.setMchntNo(mchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, wechatMchntInfo, wechatMchntInfo);
        if (0 != iReturnCode) {
            rglog.error("获取微信商户基本信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
            return null;
        }
        return wechatMchntInfo;
    }

    @Override
    public WechatMchntInfo selectWechatMchntInfoByWechatMchntNo(String instId, String wechatMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompWechatMchntInfoByWechatMchntNo");
        WechatMchntInfo wechatMchntInfo = new WechatMchntInfo();
        wechatMchntInfo.setInstId(instId);
        wechatMchntInfo.setWechatMchntNo(wechatMchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, wechatMchntInfo, wechatMchntInfo);
        if (0 != iReturnCode) {
            rglog.error("获取微信商户基本信息失败,instId:<{}>,wechatMchntNo:<{}>,iReturnCode:<{}>", instId, wechatMchntNo, iReturnCode);
            return null;
        }
        return wechatMchntInfo;
    }
}
